package mspdt.ontrade;


import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.modifier.IModifier;

import android.content.Intent;

public class GamesActivity extends BaseGameActivity{

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 720;
	private Camera mCamera;
	private Scene mainScene;

	

	
	@Override
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), mCamera);
		mCamera.setZClippingPlanes(-100, 100);
		return engineOptions;
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		// TODO Auto-generated method stub
		
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		// TODO Auto-generated method stub
		mainScene = new Scene();
		mainScene.setBackground(new Background(0, 255, 50));
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("pic/");
		
		String[] menu_images = {"ic_launcher.png", "ic_launcher.png", "ic_launcher.png", "ic_launcher.png"};
		float[] menuX = {160f-36f, 320f-36f, 160f-36f, 320f-36f};
		float[] menuY = {240f-36f, 240f-36f, 480f-36f, 480f-36f};
		for(int i=0;i<4;i++)
		{
			BitmapTextureAtlas bottlesTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 72, 72, TextureOptions.DEFAULT);
			ITextureRegion bottlesTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bottlesTextureAtlas, this, menu_images[i], 0, 0);
			bottlesTextureAtlas.load();
			Sprite menus = new Sprite(menuX[i], menuY[i], 72, 72, bottlesTextureRegion, mEngine.getVertexBufferObjectManager())
			{
				@Override
				public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
					if(TouchEvent.ACTION_UP == pSceneTouchEvent.getAction())
					{
						final Sprite temp = this;
						final Sprite selected = new Sprite(this.getX(), this.getY(), this.getTextureRegion(), this.getVertexBufferObjectManager()) {
							@Override
							protected void applyRotation(final GLState pGLState) {
								final float rotation = this.mRotation;

								if(rotation != 0) {
									final float rotationCenterX = this.mRotationCenterX;
									final float rotationCenterY = this.mRotationCenterY;

									pGLState.translateModelViewGLMatrixf(rotationCenterX, rotationCenterY, 0);
									/* Note we are applying rotation around the y-axis and not the z-axis anymore! */
									pGLState.rotateModelViewGLMatrixf(rotation, 0, 1, 0);
									pGLState.translateModelViewGLMatrixf(-rotationCenterX, -rotationCenterY, 0);
								}
							}
						};
						selected.registerEntityModifier(new SequenceEntityModifier(new IEntityModifierListener() {
							@Override
							public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
								GamesActivity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										//Toast.makeText(Game1Activity.this, "Sequence started.", Toast.LENGTH_SHORT).show();
									}
								});
							}

							@Override
							public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
								GamesActivity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										if(Integer.parseInt(temp.getUserData().toString())==0)
											startActivity(new Intent(GamesActivity.this, Game1Activity.class));
										else if(Integer.parseInt(temp.getUserData().toString())==1)
											startActivity(new Intent(GamesActivity.this, Game2Activity.class));
										else if(Integer.parseInt(temp.getUserData().toString())==2)
											startActivity(new Intent(GamesActivity.this, Game3Activity.class));
										else if(Integer.parseInt(temp.getUserData().toString())==3)
											startActivity(new Intent(GamesActivity.this, Game4Activity.class));
										mainScene.detachChild(selected);
										mainScene.attachChild(temp);
									}
								});
							}
						}, new RotationModifier(0.3f, 0, 360)));
					
						//selected.registerEntityModifier(new LoopEntityModifier(new RotationModifier(6, 0, 360)));
						mainScene.detachChild(this);
						mainScene.detachChild(temp);
						mainScene.attachChild(selected);
					}
						
					return true;
				}
			};
			menus.setUserData(i);
			mainScene.attachChild(menus);
			mainScene.registerTouchArea(menus);
		}
		//mainScene.setChildScene(menuScene, false, true, true);
		pOnCreateSceneCallback.onCreateSceneFinished(this.mainScene);
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		// TODO Auto-generated method stub
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}
}
